package io.renren.modules.sys.service.impl;

import io.renren.modules.sys.entity.SysDept1Entity;
import io.renren.modules.sys.service.SysRoleMenuService;
import io.renren.modules.sys.service.SysUserRoleService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.sys.dao.SysRole1Dao;
import io.renren.modules.sys.entity.SysRole1Entity;
import io.renren.modules.sys.service.SysRole1Service;
import org.springframework.transaction.annotation.Transactional;


@Service("sysRole1Service")
public class SysRole1ServiceImpl extends ServiceImpl<SysRole1Dao, SysRole1Entity> implements SysRole1Service {

    @Autowired
    private SysRoleMenuService sysRoleMenuService;
    @Autowired
    private SysUserRoleService sysUserRoleService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Integer parentid=(Integer)params.get("parentid");
//        Integer rolerank=(Integer)params.get("rolerank");
//        List<Integer> lkst=Arrays.asList(0,1);
//        if (rolerank==0)
//            lkst=Arrays.asList(0);
//        if (rolerank==1)
//            lkst=Arrays.asList(0,1);
//        if (rolerank==2)
//            lkst=Arrays.asList(0,1,2);

        IPage<SysRole1Entity> page = this.page(
                new Query<SysRole1Entity>().getPage(params),
                new QueryWrapper<SysRole1Entity>().eq("parent_id",parentid)
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveRole1(SysRole1Entity role1) {
        role1.setCreateTime(new Date());
        this.save(role1);

        //�����ɫ��˵���ϵ
        sysRoleMenuService.saveOrUpdate(role1.getRoleId(), role1.getMenuIdList());


    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update1(SysRole1Entity role1) {
        this.updateById(role1);

        //���½�ɫ��˵���ϵ
        sysRoleMenuService.saveOrUpdate(role1.getRoleId(), role1.getMenuIdList());


    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch1(Long[] roleIds) {
        //ɾ����ɫ
        this.removeByIds(Arrays.asList(roleIds));

        //ɾ����ɫ��˵�����
        sysRoleMenuService.deleteBatch(roleIds);



        //ɾ����ɫ���û�����
        sysUserRoleService.deleteBatch(roleIds);
    }

    public Integer queryRoleRank(Integer roleId){

        return baseMapper.queryRoleRank(roleId);
    }
}
